#include<iostream>
#include<vector>
using namespace std;


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    long long ans = 0;
    void _pathSum(TreeNode* root, int targetSum, vector<long long>& tmpVt)
    {
        if (root == nullptr) return;
        int n = tmpVt.size();
        tmpVt.push_back(root->val + tmpVt[n - 1]);
        n++;
        for (int i = 0; i < n - 1; i++) {
            long long tmpSum = tmpVt[n - 1] - tmpVt[i];
            if (tmpSum == targetSum) ans++;
        }
        _pathSum(root->left, targetSum, tmpVt);
        _pathSum(root->right, targetSum, tmpVt);
        tmpVt.pop_back();
    }

    int pathSum(TreeNode* root, int targetSum) {
        vector<long long> Vt;
        Vt.push_back(0);
        _pathSum(root, targetSum, Vt);
        return ans;
    }
};